package com.one.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.one.dto.EnumResult;
import com.one.dto.User;
import com.one.entity.*;
import com.one.enums.*;
import com.one.mapper.*;
import com.one.service.CommonService;
import com.one.dto.Result;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommonServiceImpl implements CommonService {
    private final CommonMapper commonMapper;
    private final SemesterInfoMapper semesterInfoMapper;
    private final SysClassMapper sysClassMapper;
    private final UserRoleMapper userRoleMapper;
    private final RoleIndicatorMapper roleIndicatorMapper;

    public CommonServiceImpl(SemesterInfoMapper semesterInfoMapper,CommonMapper commonMapper,SysClassMapper sysClassMapper,UserRoleMapper userRoleMapper,RoleIndicatorMapper roleIndicatorMapper) {
        this.commonMapper = commonMapper;
        this.sysClassMapper = sysClassMapper;
        this.userRoleMapper = userRoleMapper;
        this.roleIndicatorMapper = roleIndicatorMapper;
        this.semesterInfoMapper = semesterInfoMapper;
    }
    @Override
    public Result getStatus() {
        return Result.ok().data(StatusEnum.getEnumList());
    }

    @Override
    public Result getSex() {
        return Result.ok().data(SexEnum.getEnumList());
    }

    @Override
    public Result getRole() {
        return Result.ok().data(RoleEnum.getEnumList());
    }

    @Override
    public Result getIsLeafNode() {
        return Result.ok().data(IsLeafNode.getEnumList());
    }

    @Override
    public Result getClassInfo() {
        QueryWrapper<SysClassEntity> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.lambda().eq(SysClassEntity::getIsDeleted, IsDeletedEnum.NO).select(SysClassEntity::getId,SysClassEntity::getName);
        List<SysClassEntity> sysClassEntities = sysClassMapper.selectList(sectionQueryWrapper);
        if (sysClassEntities.size() == 0) {
            return Result.error().message("没有有效班级");
        }
        List<EnumResult> result = sysClassEntities.stream().map(
                entry -> new EnumResult(entry.getId(), entry.getName())).collect(Collectors.toList());
        return Result.ok().data(result);
    }

    @Override
    public Result getMeStuentOrTeacher(String userID) {
        QueryWrapper<UserRoleEntity> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.lambda().eq(UserRoleEntity::getUserId,userID).eq(UserRoleEntity::getRoleId, RoleEnum.STUDENT.getKey());
        UserRoleEntity userRoleEntity = userRoleMapper.selectOne(sectionQueryWrapper);
        if(userRoleEntity!=null){
            QueryWrapper<RoleIndicatorEntity> sectionQueryWrapper1 = new QueryWrapper<>();
            sectionQueryWrapper1.lambda().eq(RoleIndicatorEntity::getRid,userRoleEntity.getRoleId());
            RoleIndicatorEntity roleIndicatorEntity = roleIndicatorMapper.selectOne(sectionQueryWrapper1);
            return Result.ok().data(new User(userID,null,null,RoleEnum.STUDENT.getKey(),roleIndicatorEntity!=null?roleIndicatorEntity.getIid():null)) ;
        }else {
            QueryWrapper<RoleIndicatorEntity> sectionQueryWrapper1 = new QueryWrapper<>();
            sectionQueryWrapper1.lambda().eq(RoleIndicatorEntity::getRid,RoleEnum.TEACHER.getKey());
            RoleIndicatorEntity roleIndicatorEntity = roleIndicatorMapper.selectOne(sectionQueryWrapper1);
            return Result.ok().data(new User(userID,null,null,RoleEnum.TEACHER.getKey(),roleIndicatorEntity!=null?roleIndicatorEntity.getIid():null)) ;
        }
    }

    @Override
    public Result getSems() {
        QueryWrapper<SemesterInfoEntity> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.lambda().eq(SemesterInfoEntity::getIsDeleted, IsDeletedEnum.NO).select(SemesterInfoEntity::getId,SemesterInfoEntity::getName);
        List<SemesterInfoEntity> semesterInfoEntities = semesterInfoMapper.selectList(sectionQueryWrapper);
        if (semesterInfoEntities.size() == 0) {
            return Result.error().message("没有有效批次");
        }
        List<EnumResult> result = semesterInfoEntities.stream().map(
                entry -> new EnumResult(entry.getId().toString(), entry.getName())).collect(Collectors.toList());
        return Result.ok().data(result);
    }
}
